package org.queryexecutor.swing.frames;

import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JMenuItem;

import org.queryexecutor.database.QueryTableModel;
import org.queryexecutor.swing.frames.components.methods.ButtonMethods;
import org.queryexecutor.swt.frames.MainShell;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ListSelectionModel;
import javax.swing.KeyStroke;
import java.awt.event.InputEvent;
import java.awt.Panel;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame
{
	
	private static final long	serialVersionUID	= 1L;
	private JPanel				jContentPane		= null;
	private JTable resultTable;
	
	private static MainFrame mainFrame;
	private JTextArea queryArea;
	private QueryTableModel qtm;
	private JMenuItem mntmExecuteAll;
	
	public JTextArea getQueryArea()
	{
		return this.queryArea;
	}

	/**
	 * This is the default constructor
	 */
	private MainFrame()
	{
		super();
		setPreferredSize(new Dimension(700, 650));
		setMinimumSize(new Dimension(700, 650));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.qtm = new QueryTableModel();
		initialize();
	}
	
	public static MainFrame getInstance()
	{
		if(mainFrame == null){
			mainFrame = new MainFrame();
		}
		
		return mainFrame;
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(448, 304);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmExit = new JMenuItem("Exit");
		mntmExit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_MASK));
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		mnFile.add(mntmExit);
		
		JMenu mnQuery = new JMenu("Query");
		menuBar.add(mnQuery);
		
		this.mntmExecuteAll = new JMenuItem("Execute");
		mntmExecuteAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0));
		this.mntmExecuteAll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(MainFrame.this.queryArea.getSelectedText() == null || MainFrame.this.queryArea.getSelectedText().equals("")){
					ButtonMethods.getInstance().mntmExecuteAllOnActionPerformed(e, MainFrame.this.queryArea.getText(), MainFrame.this.resultTable);
				}else{
					ButtonMethods.getInstance().mntmExecuteAllOnActionPerformed(e, MainFrame.this.queryArea.getSelectedText(), MainFrame.this.resultTable);
				}
			}
		});
		mnQuery.add(this.mntmExecuteAll);
		
		JMenuItem mntmAnotherQuery = new JMenuItem("Another Query");
		mntmAnotherQuery.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Thread t = new Thread(new Runnable()
				{
					
					@Override
					public void run()
					{
						MainShell mainShell = new MainShell();
						mainShell.show();
					}
				});
				t.setDaemon(true);
				t.start();
			}
		});
		mnQuery.add(mntmAnotherQuery);
		this.setContentPane(getJContentPane());
		this.setTitle("Query Executor");
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if(this.jContentPane == null) {
			this.jContentPane = new JPanel();
			this.jContentPane.setLayout(new BorderLayout());
			
			JSplitPane splitPane = new JSplitPane();
			splitPane.setDividerSize(7);
			splitPane.setContinuousLayout(true);
			splitPane.setResizeWeight(0.5);
			splitPane.setOneTouchExpandable(true);
			splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			this.jContentPane.add(splitPane, BorderLayout.CENTER);
			
			JScrollPane queryPane = new JScrollPane();
			splitPane.setLeftComponent(queryPane);
			
			this.queryArea = new JTextArea();
			this.queryArea.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(e.getKeyCode() == KeyEvent.VK_F5){
						MainFrame.this.mntmExecuteAll.doClick();
					}
				}
			});
			this.queryArea.setWrapStyleWord(true);
			this.queryArea.setLineWrap(true);
			queryPane.setViewportView(this.queryArea);
			
			JScrollPane resultPane = new JScrollPane();
			splitPane.setRightComponent(resultPane);
			
			this.resultTable = new JTable(this.qtm);
			resultTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
			this.resultTable.setFillsViewportHeight(true);
			this.resultTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			resultPane.setViewportView(this.resultTable);
			
			JPanel panel = new JPanel();
			panel.setBorder(new EmptyBorder(5, 5, 5, 5));
			jContentPane.add(panel, BorderLayout.SOUTH);
			GridBagLayout gbl_panel = new GridBagLayout();
			gbl_panel.columnWidths = new int[]{280, 0};
			gbl_panel.rowHeights = new int[]{14, 0};
			gbl_panel.columnWeights = new double[]{0.0, Double.MIN_VALUE};
			gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
			panel.setLayout(gbl_panel);
			
			JLabel lblStatus = new JLabel("Ready");
			GridBagConstraints gbc_lblStatus = new GridBagConstraints();
			gbc_lblStatus.fill = GridBagConstraints.BOTH;
			gbc_lblStatus.gridx = 0;
			gbc_lblStatus.gridy = 0;
			panel.add(lblStatus, gbc_lblStatus);
		}
		return this.jContentPane;
	}
	
	public QueryTableModel getQueryTableModel()
	{
		return this.qtm;
	}
	
}
